package com.dogcatfishdish.depthdisplay 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	/**
	 * ...
	 * @author bb
	 */
	public class ShadowDepthItem extends DepthItem
	{
		protected var _copy:Bitmap;
		protected var _dropShadow:DropShadowFilter;
		public var blurFactor:Number = 2.0;
		
		public function ShadowDepthItem(item:DisplayObject) 
		{
			super(item);
			
			_dropShadow = new DropShadowFilter(0, 45, 0, 0.5, 0, 0, 1, 2, false, true);
			
			drawCopy();
		}
		
		override public function set z(value:Number):void {
			super.z = value;
			
			_dropShadow.distance = _z;
			_dropShadow.blurY = _dropShadow.blurX = _z / blurFactor;
			
			_copy.filters = [_dropShadow];
		}
	
		protected function drawCopy():void {
			
			if (_copy) {
				removeCopy();
			}
			
			var bmpd:BitmapData = new BitmapData(_item.width, _item.height, true, 0x00000000);
			bmpd.draw(_item, new Matrix(1, 0, 0, 1, _item.width * 0.5, _item.height * 0.5));
			
			_copy = new Bitmap(bmpd);
			_copy.x -= _copy.width * 0.5;
			_copy.y -= _copy.height * 0.5;
		
			addChildAt(_copy, 0);
			
			_copy.filters = [_dropShadow];
		}
		
		protected function removeCopy():void {
			removeChild(_copy);
			_copy.bitmapData.dispose();
			_copy = null;
		}
		
		public function set dropShadow(drop:DropShadowFilter):void {
			_dropShadow = drop.clone() as DropShadowFilter;
		}
		
		public function get dropShadow():DropShadowFilter {
			return _dropShadow;
		}
	}

}